<template>
	<view class="content">
		<image class="banner" src="/static/image/table.png"></image>
		<view class="content-bg">
			<view class="content-box">
				<view class="entry-box">
					<view class="entry-item" @click="navigateToBallroomIntro">
						<image class="entry-icon" src="/static/image/icon_pp.png"></image>
						<text>球室介绍</text>
					</view>
					<view class="entry-item" @click="openAppointmentModal">
						<image class="entry-icon" src="/static/image/icon_book.png"></image>
						<text>预约须知</text>
					</view>
					<view class="entry-item" @click="navigateToList">
						<image class="entry-icon" src="/static/image/icon_appointment.png"></image>
						<text>我的预约</text>
					</view>
				</view>
				<view class="info">
					<view class="info-item place">
						<image class="place-icon" src="/static/image/icon_place.png"></image>
						<text>场馆地址:何家湖校区图书馆1楼内</text>
					</view>
					<view class="info-item appointment-time">
						<image class="place-icon" src="/static/image/icon-time.png"></image>
						<text>预约时间: <text class="time-color">{{appointment_time}}</text></text>
					</view>
				</view>
				<view class="list-title">
					<view class="title-left">
						球桌列表
					</view>
					<view class="title-right" @click="navigateToList">
						<text>查看更多</text>
						<image class="icon" src="/static/image/arrow_right.png"></image>
					</view>
				</view>
				<view class="list-box">
					<view class="list-item" v-for="item in table_Data.slice(0,2)" :key="item.id">
						<image :src="item.imgUrl || '/static/image/table.png'"></image>
						<view class="item-info">
							<text>{{ item.name || item.productName || '未知球桌' }}</text>
							<text>{{ item.price || item.unitPrice || 0 }}元/小时</text>
							
							<view @click="goToReservation(item.id)">预定</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="modal" v-if="showModal">
			<view class="modal-content">
				<view class="modal-header">
					<text class="modal-title">预约须知</text>
					<view class="close-btn" @click="closeAppointmentModal">×</view>
				</view>
				<view class="modal-body">
					<view class="notice-content">
						<text class="notice-text">1. 本场馆开放时间：每天 8:00-22:00</text>
						<text class="notice-text">2. 预约需提前1小时以上，可预约未来7天内的场地</text>
						<text class="notice-text">3. 每次预约时长不超过2小时</text>
						<text class="notice-text">4. 请按时到场使用，如需取消请提前30分钟</text>
						<text class="notice-text">5. 使用时请爱护设施，损坏需照价赔偿</text>
						<text class="notice-text">6. 禁止携带食物进入场地，保持场地清洁</text>
						<text class="notice-text">7. 请穿着合适的运动服装和鞋子</text>
					</view>
				</view>
				<view class="modal-footer">
					<button class="confirm-btn" @click="closeAppointmentModal">确定</button>
				</view>
			</view>
		</view>
	</view>	
</template>

<script setup>
	import {
		ref
	} from 'vue';
	const appointment_time = ref('19:00-21:00');
	const showModal = ref(false)
	import {
		getProductList
	} from '@/services/product.js'
	const table_Data = ref([])
	const getList = async () => {
		try {
			const res = await getProductList()
			console.log('Product list response:', res);
			if (res && res.code === 200) {
				// 根据实际返回的数据结构调整
				table_Data.value = res.data?.list || res.data || []
			} else {
				uni.showToast({
					title: res?.message || '获取球桌列表失败',
					icon: 'none'
				})
			}
		} catch (error) {
			console.error('获取球桌列表出错:', error);
			uni.showToast({
				title: '网络请求失败',
				icon: 'none'
			})
		}
	}

	getList()

	const navigateToList = () => {
		uni.switchTab({
			url: '/pages/list/list'
		})
	}
	const navigateToBallroomIntro = () => {
		uni.navigateTo({
			url: '/pages/index/ballroomIntroduction'
		})
	}
	const openAppointmentModal = () => {
		showModal.value = true;
	}

	const closeAppointmentModal = () => {
		showModal.value = false;
	}
	
	const goToReservation = (id) => {
		uni.navigateTo({
			url: `/pages/list/appointmentDetails?id=${id}`
		})
	}
</script>

<!-- 样式部分保持不变 -->
<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #f5f5f5;

		.banner {
			width: 100%;
			height: 160px;
		}

		.content-bg {
			width: 100%;

			.content-box {
				padding: 0 15px;
				padding-top: 20px;

				.entry-box {
					background-color: #fff;
					border-radius: 15px;
					box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.25);
					display: flex;
					justify-content: space-around;
					padding: 12px;

					.entry-item {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.entry-icon {
							width: 35rpx;
							height: 35rpx;
							margin-bottom: 5px;
						}
					}
				}

				.info {
					margin: 20px 0;
					background-color: #fff;
					border-radius: 15px;
					padding: 20rpx;
					box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

					.info-item {
						display: flex;
						align-items: center;
						padding: 15rpx 0;

						&:first-child {
							padding-top: 0;
						}

						&:last-child {
							padding-bottom: 0;
						}

						&.place {
							border-bottom: 1rpx solid #f0f0f0;
						}

						.place-icon {
							width: 32rpx;
							height: 32rpx;
							margin-right: 15rpx;
							flex-shrink: 0;
						}

						text {
							font-size: 28rpx;
							color: #333;
							line-height: 1.4;
						}

						.time-color {
							color: #ff9900;
							font-weight: 500;
						}
					}
				}

				.list-title {
					margin: 20px 0;
					display: flex;
					justify-content: space-between;

					.title-left {
						display: flex;
						align-items: center;

						&::before {
							content: '';
							display: inline-block;
							width: 4px;
							height: 16px;
							background: rgba(81, 89, 245, 1);
							border-radius: 20px;
							margin-right: 5px;
						}
					}

					.title-right {
						display: flex;
						align-items: center;

						image {
							width: 15px;
							height: 15px;
							margin-left: 5px;
						}
					}
				}

				.list-box {
					.list-item {
						background-color: #fff;
						border-radius: 10px;
						display: flex;
						flex-direction: row;
						align-items: center;
						padding: 10px;
						margin-bottom: 10px;

						image {
							width: 120rpx;
							height: 120rpx;
							border-radius: 10rpx;
							margin-right: 20rpx;
						}

						.item-info {
							flex: 1;
							display: flex;
							flex-direction: column;
							justify-content: space-around;

							text:nth-child(1) {
								font-size: 32rpx;
								font-weight: bold;
								margin-bottom: 10rpx;
							}

							text:nth-child(2) {
								font-size: 28rpx;
								color: #999;
								margin-bottom: 20rpx;
							}

							view {
								width: 120rpx;
								height: 50rpx;
								background: linear-gradient(90deg, rgba(81, 89, 245, 1), rgba(134, 142, 255, 1));
								border-radius: 25rpx;
								color: #fff;
								font-size: 24rpx;
								display: flex;
								align-items: center;
								justify-content: center;
							}
						}
					}
				}
			}
		}
	}

	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;

		.modal-content {
			width: 80%;
			background-color: #fff;
			border-radius: 10px;
			overflow: hidden;

			.modal-header {
				padding: 20rpx 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid #eee;

				.modal-title {
					font-size: 36rpx;
					font-weight: bold;
				}

				.close-btn {
					font-size: 40rpx;
					color: #999;
				}
			}

			.modal-body {
				padding: 30rpx;
				max-height: 60vh;
				overflow-y: auto;

				.notice-content {
					display: flex;
					flex-direction: column;

					.notice-text {
						font-size: 28rpx;
						line-height: 1.6;
						margin-bottom: 15rpx;
					}
				}
			}

			.modal-footer {
				padding: 20rpx 30rpx;
				display: flex;
				justify-content: center;

				.confirm-btn {
					width: 100%;
					height: 80rpx;
					background: linear-gradient(90deg, rgba(81, 89, 245, 1), rgba(134, 142, 255, 1));
					border-radius: 40rpx;
					color: #fff;
					font-size: 32rpx;
				}
			}
		}
	}
</style>